{$R-}

(*
	sendSPort portNumber, addLFs, string -- Send a string to the serial port driver.
	By Harry Chesley.  DO NOT call the author!  Contact Apple Developer 
	Support on AppleLink "MacDTS" or on MCI "MacTech".

	ŠApple Computer, Inc. 1987
	All Rights Reserved.

	To compile and link this file using Macintosh Programmer's Workshop,

	pascal -w sendPort.p
	link -m ENTRYPOINT -o HyperTerm -rt XCMD=2 -sn Main=sendSPort sendPort.p.o "{MPW}"Libraries:interface.o
	
*)

{$S sendSPort } 	{ Segment name must be the same as the command name. }

unit DummyUnit;

interface

uses MemTypes, QuickDraw, OSIntf, HyperXCmd;

procedure EntryPoint(paramPtr: XCmdPtr);
	
implementation

const

return = 13;
linefeed = 10;

type

Str31 = String[31];

procedure sendSPort(paramPtr: XCmdPtr); forward;

procedure EntryPoint(paramPtr: XCmdPtr);

	begin
		sendSPort(paramPtr);
	end;

procedure sendSPort(paramPtr: XCmdPtr);

	var portNumber: integer;
		outPort: integer;
		sendLFs: boolean;
		str: Str255;
		p: Ptr;
		l: longInt;
		linefeedByte: SignedByte;

	{$I XCmdGlue.inc}

	procedure Fail(errMsg: Str255); { set theResult and quit }
		begin
			paramPtr^.returnValue := PasToZero(errMsg);
			exit(sendSPort);
		end;

	begin
		if paramPtr^.paramCount <> 3 then Fail('parameter count is not 3');

		ZeroToPas(paramPtr^.params[1]^,str);		{ First parameter is port number. }
		portNumber := StrToNum(str);
		if (portNumber < 1) or (portNumber > 2) then Fail('invalid port number');
		ZeroToPas(paramPtr^.params[2]^,str);		{ Third parameter is whether to wait. }
		sendLFs := false;
		if length(str) > 0 then
			if (str[1] = 't') or (str[1] = 'T') then sendLFs := true;
		HLock(paramPtr^.params[3]);
		p := paramPtr^.params[3]^;		{ Second parameter is string to send. }

		if portNumber = 1 then outPort := -7
		else outPort := -9;
		linefeedByte := linefeed;
		while p^ <> 0 do
			begin
				l := 1;
				if FSWrite(outPort,l,p) <> noErr then Fail('FSWrite failed');
				if l <> 1 then Fail('not all characters sent');
				if sendLFs and (p^ = return) then
					begin
						l := 1;
						if FSWrite(outPort,l,@linefeedByte) <> noErr then Fail('FSWrite failed');
						if l <> 1 then Fail('not all characters sent');
					end;
				p := ptr(ord4(p)+1);
			end;
		HUnlock(paramPtr^.params[3]);
	end;

end.
